/**
 * 
 */
package ads.engenharia.dal;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.entity.Course;
import ads.engenharia.entity.Discipline;
import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;

/**
 * @author Lanedraex
 *
 */
public class DisciplineDAO {

	private String sql;
	private String tableName = "Discipline";

	private EntityManager em = new EntityManager() {

		@Override
		public Object treatResultSet(ResultSet resultSet) throws SQLException {
			Discipline discipline = new Discipline();
			discipline.id(resultSet.getInt("id"));
			discipline.name(resultSet.getString("name"));
			discipline.duration(resultSet.getInt("duration"));
			return discipline;
		}
	};

	/**
	 * Saves discipline.
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public int save(Discipline discipline) throws SQLException {

		sql = " insert into " + tableName
				+ " ( name, duration ) values ( ?, ? ) returning id ";

		return (int) em.getSingleResult(sql, discipline.name(),
				discipline.duration());
	}

	/**
	 * Deletes discipline.
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void delete(Discipline discipline) throws SQLException {

		sql = " delete from " + tableName + " where id = ? ";

		em.executeQuery(sql, discipline.id());
	}

	/**
	 * Deletes all disciplines.
	 * 
	 * @throws SQLException
	 */
	public void deleteAll() throws SQLException {

		sql = " delete from " + tableName;

		em.executeQuery(sql);
	}

	/**
	 * Updates discipline.
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void update(Discipline discipline) throws SQLException {

		sql = " update " + tableName
				+ " set name = ?, duration = ? where id = ? ";

		em.executeQuery(sql, discipline.name(), discipline.duration(),
				discipline.id());
	}

	/**
	 * Searches for discipline.
	 * 
	 * @param discipline
	 * @return discipline
	 * @throws SQLException
	 */
	public Discipline search(Discipline discipline) throws SQLException {

		sql = " select id, name, duration from " + tableName
				+ "  where id = ? ";

		return (Discipline) em.getSingleResult(sql, discipline.id());
	}

	/**
	 * Searches all disciplines.
	 * 
	 * @return List of disciplines
	 * @throws SQLException
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Discipline> searchAll() throws SQLException {

		sql = " select id, name, duration from " + tableName;

		return (ArrayList<Discipline>) em.getResultList(sql);
	}

	public Discipline searchById(int id) throws SQLException {

		sql = " select id, name, duration from " + tableName
				+ "  where id = ? ";

		return (Discipline) em.getSingleResult(sql, id);
	}

	public Discipline searchByMaxId() throws SQLException {

		sql = " select id, name, duration from " + tableName
				+ "  where id = ( select max(id) from " + tableName + " ) ";

		return (Discipline) em.getSingleResult(sql);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<Discipline> searchByName(String name) throws SQLException {

		sql = " select id, name, duration from " + tableName
				+ "  where name = ? ";

		return (ArrayList<Discipline>) em.getResultList(sql, name);
	}

	public ArrayList<Discipline> searchByCourse(Course course)
			throws SQLException {

		return null;
	}

	public ArrayList<Discipline> searchByProfessor(Professor professor)
			throws SQLException {

		return null;
	}

	public ArrayList<Discipline> searchBySchedule(Schedule schedule)
			throws SQLException {

		return null;
	}

	/**
	 * Searches by discipline total duration.
	 * 
	 * @param duration
	 * @return List of disciplines
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByDuration(int duration)
			throws SQLException {

		return null;
	}

	/**
	 * Searches for disciplines that have professors or not.
	 * 
	 * @param status
	 * @return List of disciplines
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByStatus(boolean status)
			throws SQLException {

		return null;
	}
}
